home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / Date.C < prev    next >
C/C++ Source or Header  |  1992-05-05  |  2KB  |  117 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "Date.h"
  6.  
  7. #include "Math.h"
  8. #include "Class.h"
  9. #include "String.h"
  10.  
  11. //---- Date --------------------------------------------------------------------
  12.  
  13. Date::Date()
  14. {
  15.     /* ??
  16.     if ( gSystem )
  17.     gSystem->GetDate(sec, msec);
  18.     else {
  19.     sec= 0;
  20.     msec= 0;
  21.     }
  22.     */
  23.     sec= 0;
  24.     msec= 0;
  25. }
  26.  
  27. Date::Date(long sec, long msec)
  28. {
  29.     this->sec= sec;
  30.     this->msec= msec;
  31.     Normalize();
  32. }
  33.  
  34. void Date::Normalize()
  35. {
  36.     if ( Math::Abs(msec) >= 1000 ) {
  37.     sec= sec + msec / 1000;
  38.     msec= msec % 1000; 
  39.     }
  40.         
  41.     if ( msec < 0 ) {
  42.     sec= sec - 1; 
  43.     msec= 1000 + msec;
  44.     } 
  45. }
  46.  
  47. //---- access/conversion ----
  48.  
  49. char *Date::AsString()
  50. {
  51.     return form("%d.%d", sec, msec);
  52. }
  53.  
  54. //---- operators ----
  55.    
  56. Date Date::operator+(Date &d)
  57. {
  58.     Date r(sec + d.sec, msec + d.msec);    
  59.     return r;    
  60. }
  61.  
  62.     
  63. Date Date::operator-(Date &d)
  64. {
  65.     Date r(sec - d.sec, msec - d.msec);    
  66.     return r;
  67. }
  68.  
  69. Date Date::operator*(int i) 
  70. {
  71.     Date r(sec * i, msec * i);
  72.     return r;    
  73. }
  74.  
  75. Date Date::operator*(double f)
  76. {
  77.     Date r(int(double(sec) * f), int(double(msec) * f));
  78.     return r;
  79. }
  80.  
  81. //---- comparison ----
  82.    
  83. int Date::Compare(Date &d)
  84. {
  85.     if ( sec < d.sec ) 
  86.        return -1;
  87.     if ( sec > d.sec )
  88.     return 1;  
  89.     if ( msec < d.msec ) 
  90.     return -1;
  91.     if ( msec > d.msec )
  92.     return 1;
  93.     return 0;
  94. }
  95.  
  96. //---- Stream operators ----
  97.  
  98. OStream &operator<< (OStream &os, Date &d)
  99. {
  100.     return os << d.sec SP << d.msec;
  101. }
  102.  
  103. IStream &operator>> (IStream &is, Date &d)
  104. {
  105.     return is >> d.sec >> d.msec;
  106. }
  107.  
  108. //---- metadef ----
  109.  
  110. SimpleMetaImpl(Date)
  111. {
  112.     len= len;
  113.     isptr= isptr;
  114.     sprintf(buf, "%s", ((Date *) addr)->AsString());
  115. }
  116.  
  117.